package com.qf.news.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.news.entity.NewsComment;
import com.qf.news.mapper.NewsCommentMapper;
import com.qf.news.service.NewsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project spring_news
 * @Package com.qf.news.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/3 17:47
 */

@Service
public class NewsCommentServiceImpl implements NewsCommentService {

    @Autowired
    private NewsCommentMapper newsCommentMapper;

    @Override
    @Transactional
    public void insert(NewsComment comment) {
        int i = newsCommentMapper.insertSelective(comment);
        if (i != 1){
            throw new RuntimeException("添加评论失败");
        }
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        int i = newsCommentMapper.deleteByPrimaryKey(id);
        if (i != 1){
            throw new RuntimeException("删除评论失败");
        }
    }

    @Override
    public PageInfo<NewsComment> findByDetailIdAndPage(Integer pageNum, Integer pageSize, Integer newsId) {
        PageHelper.startPage(pageNum, pageSize);
        List<NewsComment> list = newsCommentMapper.selectByNewsId(newsId);
        PageInfo<NewsComment> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
